/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 

function BundleView(model) { 
	this.model = model; 	
}

BundleView.prototype.show = function(connection) { 				
	$("#bundleInfo").empty(); 	
	buco.propertyLayout.show("south"); 
	// Create required elements
	var treeview = $("<div id=\"treeview\"></div>"); 	
	var wrap = $("<div></div>").addClass("bundleInfoWrap"); 	
	$("#bundleInfo").append(wrap); 
	$("#bundleInfo").append(treeview); 	
	
	var bundleName = $("<p></p>").addClass("bundleName").text(this.model.bundleSymbolicName); 
	$(wrap).append(bundleName); 
	
	if(this.model.bundleVersion != null) { 
		var bundleVersion = $("<p></p>").addClass("bundleInfo").text("Version: " + this.model.bundleVersion); 
		$(wrap).append(bundleVersion); 
	}
	
	if(this.model.bundleDescription != null) { 
		var bundleDescr = $("<p></p>").addClass("bundleInfo").text(this.model.bundleDescription); 
		$(wrap).append(bundleDescr); 
	}		
	
	if(this.model.bcl != null) {
		// show semantic information
		$("#bclView").html(this.model.bcl.toHtml());
	} else { 
		$("#bclView").empty(); 
	}
	
	// Transform bundle model to a jstree object		
	var imports = []; 
	var requiredBundles = []; 
	var exports = []; 

	// Create imports and required bundles
	for(var i = 0; i < this.model.requirements.length; i++) { 
		var req = this.model.requirements[i]; 				
		if(req.type == "ImportPackageRequirement") {
			var text = req.packageName; 
			if(req.packageVersionRange != null) { 
				text += ", version=" + req.packageVersionRange; 
			}
			imports.push({data: text});			
		}
		else if(req.type = "RequireBundleRequirement") { 			
			var text = req.bundleSymbolicName; 
			if(req.bundleVersionRange != null) { 
				text += ", version=" + req.bundleVersionRange;
			}
			requiredBundles.push( {data: text} );			
		}		
	}
	
	// Create exports
	for(var i = 0; i < this.model.capabilities.length; i++) { 
		var cap = this.model.capabilities[i]; 		
		var node = {data: cap.packageName}
		if(cap.packageVersion != null) { 
			node.data += ", version=" + cap.packageVersion; 
		}
		exports.push(node);					
	}
			
	var nodes = []
	
	if(imports.length > 0) {
		nodes.push( {data: {title: "Imported Packages", attr: { "class" : "treeViewRoot" }}, children: imports, state: "open"} );
	}	
	if(exports.length > 0) {
		nodes.push( {data: {title: "Exported Packages", attr: { "class" : "treeViewRoot" } }, children: exports, state: "open"} );
	}		
	if(requiredBundles.length > 0) { 		
		nodes.push( {data: {title: "Required Bundles", attr: { "class" : "treeViewRoot" }}, children: requiredBundles, state: "open"} ); 
	}
		
	// Render tree view 
	$(treeview).jstree({ 
		json_data: {"data" : nodes},
		plugins: ["themes", "json_data", "ui"],
		themes : {
			theme: "default", 
			dots: true, 
			icons: false
		}
	});

}
